<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="header">
        <el-button type="primary" size="small" @click="openModal(null)">
          <i class="el-icon-circle-plus-outline"></i>增加
        </el-button>
        <el-button
          type="danger"
          size="small"
          @click="deBounceWrap(delClick, multipleSelection)"
          :disabled="!multipleSelection.length"
        >
          <i class="el-icon-delete"></i>删除
        </el-button>
      </div>
      <el-table
        :data="tableData"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        tooltip-effect="dark"
        ref="multipleTable"
        :span-method="objectSpanMethod"
        border
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="序号" width="100"></el-table-column>
        <el-table-column prop="title" label="分类名称"></el-table-column>
        <el-table-column prop="childTitle" label="二级分类"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="100">
          <template slot-scope="scope">
            <!--            <el-button type="success" size="small" @click="openModal(scope.row)">
              <i class="el-icon-edit-outline"></i>编辑
            </el-button>-->
            <el-button
              type="danger"
              size="small"
              @click="deBounceWrap(delClick, [scope.row])"
            >
              <i class="el-icon-delete"></i>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <TMPagination
        :current-page.sync="pageList.page"
        :total.sync="pageList.total"
        :page-size.sync="pageList.page_size"
        @updateTable="getTableData"
      />

      <category-management-modal
        @updateTable="getTableData"
        :classify-options="categoryList"
        ref="categoryManagementModal"
      />
    </el-card>
  </div>
</template>

<script>
import {
  getTableList,
  deleteType
} from "../../../../api/personalizedMall/goodNumberType";

import TMPagination from "../../wishList/childComps/TMPagination/TMPagination";
import categoryManagementModal from "./childComps/categoryManagementModal/categoryManagementModal";


export default {
  name: "goodNumberType",
  components: { categoryManagementModal, TMPagination },
  data() {
    return {
      tableData: [],
      categoryList: [],
      multipleSelection: [],
      spanArr: [],
      pos: null,
      pageList: {
        page: 1,
        page_size: 10,
        total: null
      },
      loading: false
    };
  },
  computed: {
    cateModal() {
      return this.$refs.categoryManagementModal;
    }
  },
  created() {
    this.getTableData();
  },
  methods: {
    // 点击事件
    openModal(data) {
      this.cateModal.openModal(data);
    },
    /**
     * delClick
     * @desc 点击删除按钮
     * @param idArr {Array} 行数据 [{}]
     */
    delClick(idArr) {
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let ids = "";
          idArr.map((item, index) =>
            ids += index === idArr.length - 1 ? item.childId + "" : item.childId + ","
          );
          if (ids != 'undefined') {
            const detail = await deleteType(ids);
            if (detail.code === 200) {
              this.$message({ type: "success", message: "删除成功!" });
              await this.getTableData();
            }
          } else {
            this.$message.info('没有可删除的二级分类')
          }
        })
        .catch(() => {
          this.$message({ type: "info", message: "已取消删除" });
        });
    },
    // 网路请求
    getTableData: async function() {
      this.loading = true
      this.tableData = [];
      this.loading = true;
      const data = await getTableList();
      if (data.code === 200) {
        this.categoryList = data.data;
        data.data.map(item => {
          let children = {};
          if (item.sub_cates.length) {
            item.sub_cates.map((item1, index) => {
              this.tableData.push({
                id: item.id,
                title: item.title,
                length: item.sub_cates.length ? item.sub_cates.length : 0,
                childId: item1.id,
                childTitle: item1.title
              });
            });
          } else {
            this.tableData.push({
              id: item.id,
              title: item.title
            });
          }
        });
        this.ergodicData(this.tableData);
        this.pageList.page = data.data.current_page;
        this.pageList.total = data.data.total;
        this.pageList.page_size = data.data.per_page * 1;
        this.loading = false;
      }
    },
    /**
     * objectSpanMethod
     * @desc 进行合并列
     * @param row
     * @param column
     * @param rowIndex
     * @param columnIndex
     * @returns {{colspan: number, rowspan: number}}
     */
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      let colunmIndexArr = [2];

      if (columnIndex === 2 || columnIndex === 1) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    },
    //合并行前数据整理 tableData 就是请求下来的数据
    ergodicData(tableData) {
      this.spanArr = []
      for (let i = 0; i < tableData.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          // 判断本次和上一次的 topName 值是否相等
          if (tableData[i].id === tableData[i - 1].id) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>

<style></style>
